import asyncio, pyautogui, random

from core.base_actions import BaseActions
from core.utils import get_image_pos

class AutoCraftActions(BaseActions):

  async def set_cur_fm_recipe(self):
    await self.log("set_cur_fm_recipe start")
    await asyncio.sleep(random.uniform(1, 2))
    pyautogui.press("f6")
    await asyncio.sleep(random.uniform(1, 2))
    await self.log("set_cur_fm_recipe end")

  async def start_craft(self, craft_dz, craft_fm):
    await self.log("开始制造")
    craft_pos = get_image_pos("images_2/word_craft.png")
    craft_count = 0
    while craft_pos is None:
      if craft_dz and craft_fm:
        pyautogui.press("num5" if random.random() > 0.5 else "x")
      if craft_dz and not craft_fm:
        pyautogui.press("h" if random.random() > 0.5 else "o")
      if craft_fm and not craft_dz:
        pyautogui.press("f10" if random.random() > 0.5 else "j")
      await asyncio.sleep(random.uniform(4, 5))
      craft_pos = get_image_pos("images_2/word_craft.png")
      await self.log(f"craft count {craft_count}")
      craft_count += 1
    await self.log("制造完成")

  async def send_mail(self):
    await self.log("开始发送制造物品")
    await asyncio.sleep(random.uniform(1.5, 2))
    # 使用凯蒂玩具
    pyautogui.press("num4" if random.random() > 0.5 else "z")
    await asyncio.sleep(random.uniform(1.5, 2))
    # target 凯蒂
    pyautogui.press("num4" if random.random() > 0.5 else "z")
    await asyncio.sleep(random.uniform(1.5, 2))
    # 与凯蒂交互
    pyautogui.press("9")
    await asyncio.sleep(random.uniform(1.5, 2))
    # 打开邮箱界面
    pyautogui.press("num4" if random.random() > 0.5 else "z")
    await asyncio.sleep(random.uniform(1.5, 2))
    pyautogui.press("num2" if random.random() > 0.5 else "f")
    await asyncio.sleep(random.uniform(4, 5))
    await self.log("发送制造物品完成")
    
  async def start_dz_craft(self):
    await self.log("开始锻造制造")
    craft_pos = get_image_pos("images_2/duan_C.png")
    craft_count = 0
    while craft_pos is None:
      pyautogui.press("h" if random.random() > 0.5 else "o")
      await asyncio.sleep(random.uniform(4, 5))
      craft_pos = get_image_pos("images_2/duan_C.png")
      await self.log(f"锻造craft count {craft_count}")
      craft_count += 1
    await self.log("锻造制造完成")
    
  async def start_fm_craft(self):
    await self.log("开始附魔制造")
    craft_pos = get_image_pos("images_2/fu_D.png")
    craft_count = 0
    while craft_pos is None:
      pyautogui.press("f10" if random.random() > 0.5 else "j")
      await asyncio.sleep(random.uniform(4, 5))
      craft_pos = get_image_pos("images_2/fu_D.png")
      await self.log(f"附魔craft count {craft_count}")
      craft_count += 1
    await self.log("附魔制造完成")
  